package edu.example;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;


public class viewAppTable extends JFrame
{
	private JTable appTable;
	private MyTableModel tableModel;
	private JScrollPane scrollPane;
	private JPanel p;
	private JButton backArrowButton;
	private JButton nextButton;
	private Vector<String[]> data;
	private String[] attributesName;
	
	// event handler
	private RowSelectedHandler rowSelectedHandler;
	private BackHandler backHandler;
	
	// data control
	private ApplicationManagerCloud am;
	private UserManager um;
	private selectStudentsInViewTable backScreen;
	
	
	public viewAppTable(ApplicationManagerCloud initAm, UserManager initUm, selectStudentsInViewTable initBackScreen, Vector<String> initFields, 
						Vector<Application> initStudents)
	{
		
		am = initAm;
		um = initUm;
		backScreen = initBackScreen;
		attributesName = new String[initFields.size()+1];
		attributesName[initFields.size()] = "E-Mail";
		for (int i=0; i<initFields.size(); i++)
			attributesName[i] = initFields.get(i);
		data = new Vector<String[]>();
		String[] temp = new String[initFields.size()+1];
		for (int i=0; i<initStudents.size(); i++){
			temp = catchField(attributesName, initStudents.get(i));
			data.add(temp);
		}
		layoutGUI();
	}
	
	public String[] catchField(String[] fieldName, Application initStudent){
		String[] temp = new String[fieldName.length];
		String tmp = "";
		for (int i=0; i<fieldName.length; i++){
			tmp = fieldName[i];
			if (tmp.equals("Application#"))
				temp[i]=initStudent.getApplicantClientID();
			if (tmp.equals("Name"))
				temp[i]=initStudent.getApplicantLastName();
			if (tmp.equals("E-Mail"))
				temp[i]=initStudent.getEmailAddress();
			if (tmp.equals("TOEFL"))
				temp[i]=initStudent.getTOEFLInternet();
			if (tmp.equals("IELTS"))
				temp[i]=initStudent.getIELTS();
			if (tmp.equals("GRE Verbal"))
				temp[i]=initStudent.getVerbal();
			if (tmp.equals("GRE Quantitative"))
				temp[i]=initStudent.getQuantitative();
			if (tmp.equals("GRE Analytical"))
				temp[i]=initStudent.getAnalytical();
			if (tmp.equals("PDF File"))
				temp[i]=initStudent.getRelatedFile();
			if (tmp.equals("Institution1"))
				temp[i]=initStudent.getInstitution1();
			if (tmp.equals("Institution2"))
				temp[i]=initStudent.getInstitution2();
			if (tmp.equals("Undergraduate GPA"))
				temp[i]=initStudent.getUndergradGPA();
			if (tmp.equals("Graduate GPA"))
				temp[i]=initStudent.getGradGPA();
			if (tmp.equals("Program"))
				temp[i]=initStudent.getProgram();
			if (tmp.equals("Indicate Topic"))
				temp[i]=initStudent.getIndicateTopics();
			//if (tmp.equals("Tier"))
				//temp[i]=initStudent.getTier()+"";
		}
		return temp;
	}
	
	
	public void layoutGUI()
	{
		this.setTitle("View Application Table");
		tableModel = new MyTableModel(attributesName, data);
		appTable = new JTable(tableModel);
		appTable.setSize(1280, 720);
		appTable.setAutoCreateRowSorter(true);
		scrollPane = new JScrollPane(appTable);
		p = new JPanel();
		p.add(scrollPane);
		this.add(p);
		
		ImageIcon icon = new ImageIcon("record_back.png");
		backArrowButton = new JButton(icon);
		nextButton = new JButton("Next");
		backHandler = new BackHandler();
		rowSelectedHandler = new RowSelectedHandler();
		backArrowButton.addActionListener(backHandler);
		appTable.getSelectionModel().addListSelectionListener(rowSelectedHandler);
		
		this.setLayout(new GridBagLayout());
		addComponent(backArrowButton,			this,	0,	0,	1,	1);
		addComponent(p,							this,	1,	1,	1,	1);
		
		Container c = this.getContentPane();
		c.setBackground(Color.WHITE);
		p.setBackground(Color.WHITE);
		scrollPane.getViewport().setBackground(Color.WHITE);
		appTable.getTableHeader().setBackground(Color.WHITE);
		
		// size and locate the dialog at the center of the screen
		this.pack();
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
	}
	
	// method helping GridBagLayout
	public void addComponent(	JComponent jc, Container c, 
			int x, int y, int w, int h){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	/**
	public static void main(String[] args)
	{
		//For Test Only!!!!!!
		Vector<String> fields = new Vector<String>();
		fields.add("Name");
		fields.add("E-Mail");
		fields.add("GRE Verbal");
		fields.add("GRE Quantitative");

		File file1 = new File("AY.txt");
		File file2 = new File("AY-map.txt");
		File file3 = new File("OTS.txt");
		File file4 = new File("OTS-map.txt");
		ApplicationManager formatter = new ApplicationManager(null, file1, file2, file3, file4);
		
		TreeMap<String, Application> data = formatter.getApps();
		Vector<Application> apps = new Vector<Application>();
		
		if(!data.isEmpty())
		{
			Collection<Application> temp = data.values();
			Iterator<Application> it = temp.iterator();
			
			while(it.hasNext())
			{
				Application tempApp= it.next();
				apps.add(tempApp);
			}
		}
		
		viewAppTable temp = new viewAppTable(formatter, fields, apps);
		temp.setVisible(true);
	}
	*/
	
	class BackHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) 
		{
			viewAppTable.this.setVisible(false);
			backScreen.setVisible(true);
		}
	}
	
	class RowSelectedHandler implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent arg0) {
			// TODO Auto-generated method stub
			int row = appTable.getSelectedRow();
			if (row != -1)
			{
				Application temp = tableModel.getSelectedApp(row);
				ApplicationManager ap = ApplicationManager.getAppManager();
				viewAppFrame tempVAF = new viewAppFrame(temp, ap.getAllScore().get(temp.getEmailAddress()), um);
				tempVAF.setVisible(true);
				viewAppTable.this.setVisible(false);
			}
		}
		
	}
	
	class MyTableModel extends AbstractTableModel{

		private String[] columns;
		private Vector<String[]> data;
		
		public MyTableModel(String[] initCol, Vector<String[]> initData){
			columns = initCol;
			data = initData;
		}
		@Override
		public int getColumnCount() {
			// TODO Auto-generated method stub
			return columns.length;
		}

		@Override
		public int getRowCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public String getValueAt(int row, int col) {
			// TODO Auto-generated method stub
			return data.get(row)[col];
		}
		
		public void setValueAt(String s, int row, int col) {
			// TODO Auto-generated method stub
			data.get(row)[col] = s;
		}
		
		public String getColumnName(int col){
			return columns[col];
		}
		
		public Application getSelectedApp(int row){
			String email = getValueAt(row, columns.length-1); 
			Application app = am.getAllApps().get(email);
			return app;
		}
		
	}
}
